# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from fastapi import APIRouter, Request, Depends
from hypertext import R, PagingResult, response_json
from apps.api.schemas import user_schema as schema
from apps.api.service.user_service import UserService

router = APIRouter(prefix="/user", tags=["用户接口"])


@router.get("/center", summary="个人中心", response_model=R[schema.UserCenterVo])
@response_json
async def center(request: Request):
    user_id: int = int(request.state.user_id)
    return await UserService.center(user_id)


@router.get("/wallet", summary="余额明细", response_model=R[PagingResult[schema.UserWalletLogsVo]])
@response_json
async def wallet(request: Request, params: schema.UserWalletSearchIn = Depends()):
    user_id: int = int(request.state.user_id)
    return await UserService.wallet(user_id, params)


@router.get("/quota", summary="配额详情", response_model=R[PagingResult[schema.UserWalletLogsVo]])
@response_json
async def quota(request: Request):
    user_id: int = int(request.state.user_id)
    return await UserService.quota(user_id)


@router.post("/edit", summary="编辑信息", response_model=R)
@response_json
async def edit(request: Request, params: schema.UserEditIn):
    user_id: int = int(request.state.user_id)
    return await UserService.edit(
        params.field,
        params.value,
        user_id
    )


@router.post("/forget_pwd", summary="找回密码", response_model=R)
@response_json
async def forget_pwd(params: schema.UserForgetPwdIn):
    return await UserService.forget_pwd(
        params.scene,
        params.code,
        params.account,
        params.password
    )


@router.post("/change_pwd", summary="修改密码", response_model=R)
@response_json
async def change_pwd(request: Request, params: schema.UserChangePwdIn):
    user_id: int = int(request.state.user_id)
    return await UserService.change_pwd(
        params.old_pwd,
        params.new_pwd,
        user_id
    )


@router.post("/bind_mobile", summary="绑定手机", response_model=R)
@response_json
async def bind_mobile(request: Request, params: schema.UserBindMobileIn):
    user_id: int = int(request.state.user_id)
    return await UserService.bind_mobile(
        params.scene,
        params.mobile,
        params.code,
        user_id
    )


@router.post("/bind_email", summary="绑定邮箱", response_model=R)
@response_json
async def bind_email(request: Request, params: schema.UserBindEmailIn):
    user_id: int = int(request.state.user_id)
    return await UserService.bind_email(
        params.scene,
        params.email,
        params.code,
        user_id
    )


@router.post("/bind_wechat", summary="绑定微信", response_model=R)
@response_json
async def bind_wechat(request: Request, params: schema.UserBindWechatIn):
    user_id: int = int(request.state.user_id)
    terminal: int = request.state.terminal
    return await UserService.bind_wechat(
        params.state,
        params.code,
        user_id,
        terminal
    )
